package com.luban.editor;

import com.luban.entity.User;
import org.springframework.core.convert.TypeDescriptor;
import org.springframework.core.convert.converter.ConditionalGenericConverter;

import java.util.Collections;
import java.util.Set;

/**
 * @Author gary <498982703@qq.com>
 * @Version V1.0.0
 * @Since 1.0
 * @Date 2020/10/27
 *
 * spring 提供的类转换工具
 */
public class StringToUserConverter implements ConditionalGenericConverter {
	@Override
	public boolean matches(TypeDescriptor sourceType, TypeDescriptor targetType) {
		return sourceType.getType().equals(String.class)&& targetType.getType().equals(User.class);
	}

	@Override
	public Set<ConvertiblePair> getConvertibleTypes() {
		return Collections.singleton(new ConvertiblePair(String.class, User.class));
	}

	@Override
	public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
		User user	= new User();
		user.setName((String) source);
		return user;
	}
}
